// pages/order-list/order-list.js
const request = require('../../utils/request.js')
const api = require('../../config/api.js');
const { getStorage, setStorage, removeStorage, getBarAndTitleHeight } = require('../../utils/utils.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // tab切换变量 10待接单 20订单取消
    isShow:10,
    totalPages:0,
    currentPage:1,
    orderList: [],
    statusList:[
      {status:10,label:'待接单'},
      {status:40,label:'待上门'},
      {status:20,label:'已取消'},
      {status:30,label:'已关闭'},
      {status:3,label:'待付款'},
      {status:50,label:'已完成'},
    ],
    statusObj: { 10: '待接单', 20: '已取消', 40: '待上门', 30: '已关闭', 50: '已完成' },
    typeObj: { 1: '大件', 2: '家电', 3: '衣物', 5: '上门' },
    loadDown:false,
    selectId: ''
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.modal = this.selectComponent('#modal')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      ...getBarAndTitleHeight(),
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      isShow: getStorage('orderTabSelect') || 10,
      orderList: [],
      currentPage: 1,
      totalPages: 1,
    })
    removeStorage('orderTabSelect')
    this.getMyOrderList(1)
  },

  /**
   * 获取我的订单
   */
  async getMyOrderList(currentPage){
    let that = this
    let list = that.data.orderList
    let { isShow } = that.data
    if(currentPage ==1){
      list =[];
    }
    wx.showLoading({
      title: '加载中',
    })

    const res = await request.get(api.orderList,{
      status: isShow,
      page: currentPage
    });
    if (res.httpStatus === 200) {
      list = list.concat(res.data.records)
      that.setData({
        orderList:list,
        totalPages:res.data.pages,
        loadDown:true
      })
      wx.hideLoading()
    }
  },

  /**
   * 跳转详情页面
   */
  handleDetail(e) {
    const id = e.currentTarget.dataset.id;
    setStorage('orderTabSelect', this.data.isShow)
    wx.navigateTo({
      url: `/pages/order-detail/order-detail?id=${id}`,
    })
  },
  /**
   * 取消取消订单
   */
  handleCancel(e) {
    const item = e.currentTarget.dataset.item;
    this.setData({
      selectId: item.id
    })
    this.modal.show()
  },
  async toCancle(){
    const data = await request.post(`${api.cancleOrder}`,{
      id: this.data.selectId
    });
    this.modal.hide()
    if (data.httpStatus === 200) {
      wx.showToast({
        title: '订单取消成功',
        icon: 'none',
      });
      this.setData({
        isShow: 10,
        orderList: [],
        currentPage: 1,
        totalPages: 1,
      })
      // 调用获取优惠劵列表，重新刷新页面
      this.getMyOrderList(1)
    }
    if (data.httpStatus === 500) {
      setTimeout(() => {
        this.setData({
          isShow: 10,
          orderList: [],
          currentPage: 1,
          totalPages: 1,
        })
        // 调用获取优惠劵列表，重新刷新页面
        this.getMyOrderList(1)
      }, 500)
    }
  },
    /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let {currentPage,totalPages} = this.data
    if(currentPage<totalPages){
      currentPage++
      this.setData({
        currentPage
      })
      this.getMyOrderList(currentPage)
    }
  },
  // 1.0 Tab未兑换点击事件
  tabChange(e){
    let index = e.currentTarget.dataset.index;
    this.setData({
      isShow: Number(index),
      orderList: [],
      currentPage: 1,
      totalPages: 1,
    })
    if( Number(index) === 3) return 
      // 调用获取优惠劵列表，重新刷新页面
    this.getMyOrderList(1)
  },
})